﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using Aulac.Srv.Db.Provider; using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Data.Catalogs;
using Aulac.Srv.Db.Common;

namespace Aulac.Srv.Db.Data.Ems.Systems
{
	public class ClsSystem: DbDataCatalogs
	{
		public ClsSystem(IDbAulacConnect p_Db) :base(p_Db){}

		public XmlDataDocument  GetAccessRight(XmlDataDocument xmlDoc)
		{
			string UserName = XmlProcess.GetValueParam(xmlDoc, tagSystem.Users.UserName);
			
			string ModuleID = XmlProcess.GetValueParam(xmlDoc, tagSystem.Users.ModuleID);
						
			sqlQuery = "SELECT Sys_UserGrp.UserGrpID, Sys_UserGrp.IsAccRight, Sys_UserGrp.IsAdd, Sys_UserGrp.IsEdit, Sys_UserGrp.IsDelete, Sys_UserGrp.IsPrint, " +
                      "Sys_UserGrp.IsShow, Sys_UserGrp.IsSystem, Sys_Modules.ModuleID, Sys_Modules.ModuleName, Sys_Modules.IsActive, Sys_Modules.Location, " +
                      "Sys_Modules.ModuleType " + 
						"FROM         Sys_UserGrp INNER JOIN " +
                      "Sys_Modules ON Sys_UserGrp.ModuleID = Sys_Modules.ModuleID INNER JOIN " +
                      "Sys_UserInfo ON Sys_UserGrp.UserGrpID = Sys_UserInfo.UserGrpID " +
						"WHERE (Sys_Modules.ModuleID LIKE ? AND Sys_UserInfo.UserName LIKE ?)";
            DataTable tb = null;
            DbDataProvider.ExecuteReaderDataToTable(mDbCmd, sqlQuery,ref tb, UserName, ModuleID);
            XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
            DbDataProvider.GetValuesToChildXml(tb, ref eLines, tagCtrl.LINE, 
					tagSystem.Users.ModuleID,
					tagSystem.Users.ModuleName,
					tagSystem.Users.UserGrpID, 
					tagSystem.Users.IsAccRight,
					tagSystem.Users.IsAdd,
					tagSystem.Users.IsDelete,
					tagSystem.Users.IsEdit,
					tagSystem.Users.IsPrint,
					tagSystem.Users.IsShow,
					tagSystem.Users.IsSystem,
					tagSystem.Users.Location);
			
			return xmlDoc;
 		}

		public XmlDataDocument AddUserAccess(string UserName, string logTime)
		{
            sqlQuery = "INSERT INTO Sys_UserAccess(UserName, DateLogin) VALUES(?, ?)";
			DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery, UserName, logTime);
			return null;
		}
		#region Option and Copmpany methods
		public XmlDataDocument GetSystemOption() 
		{
			try
			{

				sqlQuery = "SELECT TOP 1 * , getDate() as SystemDate FROM Sys_Options";


				XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery);
                

				if (DbDataReader.Read()) 
				{
					XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                    DbDataProvider.GetValuesToXml(DbDataReader, ref eHeader,
                        new string[]{Options.tagOption.IsCommaSeperate,
									Options.tagOption.IsDayMonth,
										Options.tagOption.DecimalRound,
										Options.tagOption.DecimalRoundReport,
										Options.tagOption.MethodOnHand,
										Options.tagOption.AllowOverOnhand,
										Options.tagOption.RoundReportNumber,
										Options.tagOption.IsUseConvertUnit,
										Options.tagOption.IsUserMngtID, 
										Options.tagOption.LanguageView,
                                        Options.tagOption.SystemDate
                        });
				}
				DbDataReader.Close();
                
				return xmlDoc;
			}
			catch(Exception ex)
			{
                return null;
			}
		}

		public XmlDataDocument SetSystemOption(XmlDataDocument doc) 
		{
			try
			{
                string[] arr = new string[]{ Options.tagOption.DecimalRound, Options.tagOption.DecimalRoundReport, Options.tagOption.IsCommaSeperate, Options.tagOption.IsDayMonth, Options.tagOption.IsUserMngtID, Options.tagOption.IsUseConvertUnit, Options.tagOption.AllowOverOnhand, Options.tagOption.MethodOnHand, Options.tagOption.LanguageView};
                BeginTransaction(ref mDbTransaction);
				    
                sqlQuery = SqlHelper.CreateSqlDelete(TableNames.Sys_Options);
                DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery);

                XmlElement eHeader = XmlProcess.GetVchHeaderElm(doc);
                
                sqlQuery = SqlHelper.CreateSqlInsert(TableNames.Sys_Options, arr);
                DbDataProvider.AddParamToCmd(mDbCmd, eHeader, arr);
                DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery);
			}
			catch(Exception ex)
			{
				throw ex;
			}
            return doc;
		}

		#endregion

		public string GetUserWorking(XmlDataDocument inDoc)
		{
            return null;			
		}

        public XmlDataDocument CheckUserLogin(XmlDataDocument xmlDoc)
        {
            try
            {
                string userName = XmlProcess.GetValueParam(xmlDoc, tagSystem.Users.UserName);
                string password = XmlProcess.GetValueParam(xmlDoc, tagSystem.Users.UserPassword);
                string loginTime = XmlProcess.GetValueParam(xmlDoc, tagSystem.Users.DateLogin);

                sqlQuery = "SELECT	UserName, UserPassword, UserGrpID " +
                    "FROM Sys_UserInfo WHERE UserName like ? and UserPassword like ?";
                DbDataReader = DbDataProvider.ExecuteReader(mDbCmd, sqlQuery, userName, password);
                XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                bool isLogin = false;

                if (DbDataReader.Read())
                {
                    XmlProcess.AddValueParamItem(xmlDoc, tagSystem.Users.UserGrpID, DbDataProvider.GetValue(DbDataReader, tagSystem.Users.UserGrpID));
                    eHeader.SetAttribute(tagSystem.Users.UserGrpID, DbDataProvider.GetValue(DbDataReader, tagSystem.Users.UserGrpID));
                    eHeader.SetAttribute(tagSystem.Users.UserName, DbDataProvider.GetValue(DbDataReader, tagSystem.Users.UserName));
                    eHeader.SetAttribute(tagSystem.Users.UserPassword, DbDataProvider.GetValue(DbDataReader, tagSystem.Users.UserPassword));

                    isLogin = true;
                }
                DbDataReader.Close();

                if (!isLogin)
                    throw new Exception("Tên sử dụng hoặc mật khẩu không đúng !");


                try
                {
                    AddUserAccess(userName, loginTime);

                }
                catch
                {

                }
                return (new UserInfo(this.DbDataProvider.DbAulacConnection )).GetUserInfo(xmlDoc);
            }
            catch (Exception ex)
            {
                throw new Exception("Tên sử dụng hoặc mật khẩu không đúng !" + ex.Message);
            }
        }


        public XmlDataDocument ChangePassword(XmlDataDocument xmlDoc)
        {

            try
            {
                XmlNode node = XmlProcess.GetVchLineItemFirstElm(xmlDoc);
                string userName = XmlProcess.GetValueAtrb(node, tagSystem.Users.UserName);
                if (userName != null)
                {
                    sqlQuery = "Update Sys_UserInfo SET UserPassword = ?  WHERE UserName like ?";
                    DbDataProvider.ExecuteNonQuery(ref mDbCmd, sqlQuery, XmlProcess.GetValueAtrb(node, tagSystem.Users.UserPassword), userName);
                }

                return null;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

	}
}
